﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TravelLib
{

    public class FlightWrapper
    {
        public string DepartureAirportName
        {
            get;
            set;
        }

        public string DestinationAirtportName
        {
            get;
            set;
        }

        public DateTime DteTo
        {
            get;
            set;
        }
        public DateTime DteFrom
        {
            get;
            set;
        }

        public Route Route()
        {
            return (new Route(new RouteWrapper()
            {
                DestinationAirtportName=this.DestinationAirtportName,
                DepartureAirportName=this.DepartureAirportName,
                DteTo = this.DteTo,
                DteFrom = this.DteFrom
            }));
        }

        public string UID
        {
            get;
            set;
        }
        public string CompanyName
        {
            get;
            set;
        }
        public int UnitCost
        {
            get;
            set;
        }

        public string TRAVELCLASSES;
        public DateTime DepartureTime
        { get; set; }

        public DateTime ArrivalTime
        { get; set; }
        public FlightWrapper()
        {

        }
        public List<FlightClass> TravelClasses()
        {
            List<FlightClass> fc = new List<FlightClass>();
            foreach (string s in TRAVELCLASSES.Split(new char[]{'$'},StringSplitOptions.RemoveEmptyEntries))
            {
                TravelClassEnum en = TravelClassEnum.ECONOMY_CLASS;
                switch (s.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries)[0])
                {
                    case "FIRST CLASS": en = TravelClassEnum.FIRST_CLASS;
                        break;
                    case "BUSSINESS CLASS": en = TravelClassEnum.BUSSINESS_CLASS;
                        break;
                    case "ECONOMY CLASS": en = TravelClassEnum.ECONOMY_CLASS;
                        break;
                }
                fc.Add(new FlightClass()
                {
                    SeatsAvailable = Int32.Parse(s.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[1]),
                    TravelClass = en
                });
            }
            return fc;
        }
        public FlightWrapper(Flight f)
        {
            DteFrom = f.Route.DteFrom;
            DteTo = f.Route.DteTo;
            DestinationAirtportName = f.Route.DestinationAirtportName;
            DepartureAirportName = f.Route.DepartureAirportName;
            UID = f.UID;
            this.CompanyName = f.Company.CompanyName;
            DepartureTime = f.DepartureTime;
            ArrivalTime = f.ArrivalTime;
            TRAVELCLASSES = string.Empty;
            this.UnitCost = f.UnitCost;
            foreach (FlightClass ff in f.TravelClasses)
            {
                TRAVELCLASSES += StringEnum.GetStringValue(ff.TravelClass) + "|" + ff.SeatsAvailable + "$";
            }
        }
    }

    public class Flight
    {
        private Route _route;

        public Route Route
        {
            get { return _route; }
            set { _route = value; }
        }

        public string UID
        {
            get;
            set;
        }

        private int _unitCost;

        public int UnitCost
        {
            get
            {
                return _unitCost;
            }
            set
            {
                _unitCost = value;
                UpdateTravelClassCosts();
            }
        }

        public DateTime DepartureTime
        { get; set; }

        public DateTime ArrivalTime
        { get; set; }

        private List<FlightClass> _travelClasses;

        public Flight()
        {
            _travelClasses = new List<FlightClass>();
        }

        public Flight(FlightWrapper f)
        {
            _travelClasses = new List<FlightClass>();
            this.Route = f.Route();
            this.UID = f.UID;
            this._unitCost = f.UnitCost;
            this.DepartureTime = f.DepartureTime;
            this.ArrivalTime = f.ArrivalTime;
            this.Company = CompanyManager.Companies.First(x => x.CompanyName == f.CompanyName);
            this._travelClasses = f.TravelClasses();
            UpdateTravelClassCosts();
        }
        public List<FlightClass> TravelClasses
        {
            get
            {
                return _travelClasses;
            }
            set
            {
                _travelClasses = value;
                UpdateTravelClassCosts();
            }
        }
        public Company Company
        {
            get;
            set;
        }
        /// <summary>
        /// Get the FLIGHT COST per seat on the specified travelClass
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public double GetTravelClassCost(TravelClassEnum t)
        {
            double cost = 0;
            foreach (FlightClass f in TravelClasses)
            {
                if(StringEnum.GetStringValue(f.TravelClass).CompareTo(StringEnum.GetStringValue(t)) == 0)
                    return f.Cost;
            }
            return cost;
        }

        public bool IsEqual(Flight f)
        {
            if (this.UID.CompareTo(f.UID) != 0 || this.DepartureTime.CompareTo(f.DepartureTime) != 0 || this.ArrivalTime.CompareTo(f.ArrivalTime) != 0)
                return false;
            return true;
        }
        public bool IsEqual(FlightWrapper f)
        {
            if (this.UID.CompareTo(f.UID) != 0 || this.DepartureTime.CompareTo(f.DepartureTime) != 0 || this.ArrivalTime.CompareTo(f.ArrivalTime) != 0)
                return false;
            return true;
        }
        /// <summary>
        /// Update the costs per class (index of class * unitcost)
        /// </summary>
        private void UpdateTravelClassCosts()
        {
            foreach (FlightClass t in TravelClasses)
            {
                switch (t.TravelClass)
                {
                    case TravelClassEnum.BUSSINESS_CLASS: t.Cost = 10 * UnitCost;
                        break;
                    case TravelClassEnum.FIRST_CLASS: t.Cost = 7 * UnitCost;
                        break;
                    case TravelClassEnum.ECONOMY_CLASS: t.Cost = 5 * UnitCost;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
